home *** CD-ROM | disk | FTP | other *** search
- /*
- HASMiscEvents.c from Hsoi's App Shell © 1995-1997 John C. Daub. All rights reserved.
-
- This file contains various and sundry event handlers for events
- that don't need much to handle.
-
- Just a file to keep all those little things in order. Bigger event
- handlers will get their own files.
- */
-
- #pragma mark ••• #includes •••
-
- #ifndef _WASTE_
- #include "WASTE.h"
- #endif
- #include "HASGlobals.h"
- #ifndef __HSOIS_APP_SHELL__
- #include "HASMain.h"
- #endif
- #include "HASDialogs.h"
- #include "HASMenus.h"
- #include "HASMiscEvents.h"
- #include "HASWindows.h"
- #include "HASUtilities.h"
- #include "HASSoundSpeech.h"
-
- #include "WASTE_Objects.h"
-
- #ifndef __NOTIFICATION__
- #include "Notification.h"
- #endif
-
- #if HAS_DEBUG
- #include "HASUtilTest.h"
- #endif
-
- #pragma mark -
-
-
- /*
- * This, obviously, is what happens when nothing is happening...for nullEvents.
- */
-
- void HsoiDoIdle( void )
- {
- WindowRef window;
-
- // see if we have a window up...
-
- window = FrontWindow();
-
- // if we have a window, and it's a text editing window, call some WASTE routines to
- // handle such things as cursor blinking.
-
- if ( (window != nil) &&
- ( HsoiIsDocumentWindow(window) || HsoiIsClipboardWindow(window)) && !gHiliting)
- WEIdle( &gSleepTime, HsoiGetWindowWE( window ) );
- else
- // give up the processor to other applications cause we obviously don't need it
- gSleepTime = MAXLONG;
-
- return;
- }
-
- /*
- * This is called from the event dispatcher when a keyDown event occurs. Here we decide
- * just what to do with the key.
- */
-
- void HsoiDoKey( char key, const EventRecord *event )
- {
- WindowRef window;
- SignedByte keyCode;
-
- window = FrontWindow();
-
- // do nothing if no window is active
-
- if ( window == nil )
- return;
-
- // extract virtual key code from event record
-
- keyCode = BSR( BAND(event->message, keyCodeMask ), 8 );
-
- // page movement keys are handled by HsoiDoScrollKey()
-
- switch ( keyCode )
- {
- case keyPgUp:
- case keyPgDn:
- case keyHome:
- case keyEnd:
- HsoiDoScrollKey( keyCode, window );
- break;
-
- default:
- WEKey( key, event->modifiers, HsoiGetWindowWE(window) );
- break;
- }
-
- return;
- }
-
- /*
- * This, obviously, takes care of any and all (well, most) update events.
- */
-
- void HsoiDoUpdate( WindowRef window )
- {
- GrafPtr savePort;
- RgnHandle updateRgn;
-
- // if we have no windows, there's nothing to update!
-
- if ( window == nil )
- return;
-
- // if it's a dialog window, we won't handle updating it here...there are
- // dialog filter routines that handle this.
-
- if ( HsoiIsDialogWindow( window ) )
- return;
-
- // and if we're testing, no updating here. however, we still have to do
- // the updating "routine" so that stuff in the background (the desktop,
- // Finder windows, stuff from backgrounded apps, etc) gets updated.
-
- #if HAS_DEBUG
- if ( HsoiIsTestWindow( window ) )
- {
- GetPort( &savePort );
- SetPortWindowPort( window );
-
- BeginUpdate( window );
- EndUpdate( window );
-
- SetPort( savePort );
-
- return;
- }
- #endif
-
- // and now, if it's an app window, let's do something with it.
-
- // save the old drawing port
-
- GetPort( &savePort );
- SetPortWindowPort( window );
-
- // notify everything that we're doing an update.
-
- BeginUpdate( window );
-
- // BeginUpdate sets the window visRgn to the region to update
-
- updateRgn = GetWindowPort(window)->visRgn;
- if ( !EmptyRgn( updateRgn ) ) // if it's not an empty region, let's update it!
- {
- // erase update region
- EraseRgn( updateRgn );
-
- // draw scroll bars
- UpdateControls( window, updateRgn );
-
- // draw grow icon
- HsoiMyDrawGrowIcon( window, false );
-
- // draw text
- WEUpdate( updateRgn, HsoiGetWindowWE(window) );
-
- // and if it's the clipboard window, gotta do a couple extra things to update it
-
- if ( HsoiIsClipboardWindow( window ) )
- HsoiDoClipboardUpdate( window );
- }
-
- // tell everything we're done updating
-
- EndUpdate( window );
-
- // restore the old graphics port
-
- SetPort( savePort );
-
- return;
- }
-
- /*
- * This take care of activation (osEvt's). stuff to make the windows all look right and
- * proper when in the foreground or background.
- */
-
- void HsoiDoActivate( Boolean activFlag, WindowRef window )
- {
- DocumentHandle hDocument;
- WEReference we;
- GrafPtr savePort;
- ControlPartCode barHilite;
- Rect barRect;
-
- // if there aren't any windows, nothing to do here...
-
- if (window == nil)
- return;
-
- // if there's a dialog, just make sure the menus are proper, the toolbox tends to
- // handle the rest via our dialog filter routines.
-
- if ( HsoiIsDialogWindow( window ) )
- {
- HsoiAdjustMenus();
- return;
- }
-
- // if the test window, just make the menus look right
-
- #if HAS_DEBUG
-
- if ( HsoiIsTestWindow( window ) )
- {
- HsoiAdjustMenus();
- return;
- }
-
- #endif
-
- // now take care of the rest of the situations...
-
- hDocument = HsoiGetWindowDocument(window);
- we = (*hDocument)->we;
-
- // set up the port
-
- GetPort( &savePort );
- SetPortWindowPort( window );
-
- // activate or deactivate the text (and any other relevant stuff) depending on just
- // what we're doing here...
-
-
- if ( activFlag )
- {
- WEActivate( we );
- barHilite = kControlNoPart;
- }
- else
- {
- WEDeactivate( we );
- barHilite = kControlDisabledPart;
- }
-
-
- // redraw the grow icon (and validate it's rect)
-
- HsoiMyDrawGrowIcon( window, true );
-
- // redraw the scroll bars with the new highlighting (and validate their rects)
-
- // first, the vertical scroll bar
-
- HiliteControl( ((*hDocument)->scrollBars).v, barHilite );
- HsoiCalcScrollBarRect(window, v, &barRect );
- ValidRect( &barRect );
-
- // now the horizontal scroll bar
-
- HiliteControl( ((*hDocument)->scrollBars).h, barHilite );
- HsoiCalcScrollBarRect( window, h, &barRect );
- ValidRect( &barRect );
-
- // dim or undim text-related menus
-
- HsoiAdjustMenus();
-
- // restore the old graphics port..
-
- SetPort( savePort );
-
- return;
- }
-
-
- #pragma mark -
- #pragma mark ••• Notification •••
-
-
- // Following here are a few routines to handle Notification Manager stuff. It's in this
- // file cause it deals with suspend and resume events.
-
- /*
- * This function is called when we want to post a notification message to the user.
- * it sticks the message in the queue.
- */
-
- void HsoiPostNMMessage( void )
- {
- OSErr err;
-
- // tho probably not necessary to check for this, can't hurt to do
- // if they don't have the notification manager on the computer, obviously we can't
- // post a notification message...plus, you shouldn't post a notification request unless
- // the program is in the background...if it's in the foreground, they're working with it
- // already, use a dialog or something else...Notificaiton Manger stuff will still work
- // regardless if the app is front or backgrounded, but it's just good programming
- // practice to use the Notificatino Manager only when you need to alert the user to
- // something that isn't in their face.
-
- if ( !gInBackground || !gHasNotification )
- return;
-
- // set things that need to be set
-
- gNotifyRec.qType = nmType; // identifies NMRec as a notification request queue element
- gNotifyRec.nmResp = 0L; // no response procedure, we'll remove stuff in the resume event
- gNotifyRec.nmRefCon = 0L; // nothing exciting to store in the refcon
-
- // set things according to the user's preferences...
-
- if ( gMyPrefs.useIconNM )
- {
- gNotifyRec.nmIcon = GetResource( TYPE_SMALL_ICON, rNotifyIcon );
- HNoPurge( gNotifyRec.nmIcon );
- }
- else
- gNotifyRec.nmIcon = nil;
-
- gNotifyRec.nmMark = gMyPrefs.useDiamondNM;
-
- if ( gMyPrefs.useSoundNM )
- {
- gNotifyRec.nmSound = GetResource( TYPE_SOUND, rAboutSoundUhh ); // or could set sound to -1L to play the sys beep
- HNoPurge( gNotifyRec.nmSound );
- }
- else
- gNotifyRec.nmSound = nil;
-
- if ( gMyPrefs.useAlertNM )
- {
- GetIndString( gNotifyStr, rNotificationManagerStrings, strNMBringToFront );
- gNotifyRec.nmStr = gNotifyStr;
- }
- else
- gNotifyRec.nmStr = nil;
-
- // tho this is most likely unnecessary to do (notification requests should only
- // get posted if your application is in the background, and if our app is in
- // the background, we've already stopped any playing sound(s) upon the suspend
- // event), i still want to have a check in here for it. it can't hurt that
- // much, and who knows...maybe some strange scripting or other AppleEvent
- // might cause us to need this.
-
- // i just want a sound to stop playing cause we'll be using notification
- // sounds, the alert can use a sound, etc....don't want to mix and mush up sounds
-
- if ( SoundIsPlaying() )
- StopCurrentSound();
-
- // now let's actually stick the notification request into the queue
-
- err = NMInstall( &gNotifyRec );
-
- if ( err != noErr )
- SysBeep( 5 ); // lame error handling, but for now....NMInstall should almost never fail
-
- // and set a global boolean for us to keep track of stuff...
-
- gNotifying = true;
-
- return;
- }
-
-
- /*
- * The above function puts a message in the queue, this removes it (after the user takes
- * care of returning to the program)
- */
-
- void HsoiRemoveNMMessage( void )
- {
- // if the computer doesn't even have the Notificaiton Manager, forget it.
-
- if ( !gHasNotification )
- return;
-
- // if we are notifying them (there is a message in the queue)...
-
- if ( gNotifying )
- {
- // ... remove it!
-
- NMRemove( &gNotifyRec );
-
- // if we had to grab some resources to make our notification request, let's
- // make sure to free up their memory.
-
- if ( gNotifyRec.nmIcon )
- HsoiForgetResource( &gNotifyRec.nmIcon );
-
- if ( gNotifyRec.nmSound )
- HsoiForgetResource( &gNotifyRec.nmSound );
-
- // set our boolean back to an appropriate value...
-
- gNotifying = false;
-
- // throw up a quicky alert to say just why i notified you in the first place...
- // (you probably won't use something like this normally, I'm just doing this
- // as a lame way of showing you a way to impliment your own "response" procedure)
-
- // make sure the cursor is kosher
-
- InitCursor();
-
- // throw up the alert (i just used NoteAlert() cause it's simple and easy)
-
- NoteAlert( rNMRespAlert, HsoiGetMyStandardDialogFilter() );
- }
-
- return;
- }